package org.abc.uiCore.list
{
	import de.polygonal.ds.DLL;
	import de.polygonal.ds.DLLNode;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import org.abc.interfaces.IDispose;
	import org.abc.manager.ExclusionManager;
	import org.abc.uiCore.event.ListModelEvent;
	import org.abc.uiCore.vo.CpVo;

	/**
	 * @describe  	List对应的数据源
	 * @author  	Mr.zheng
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		2011-8-18 21:39
	 */
	public class ListModel extends DLL implements IDispose,IEventDispatcher
	{

		private var _exclusionManager:ExclusionManager;
		private var _dispatcher:EventDispatcher;

		public function ListModel(reservedSize:int=0, maxSize:int=-1)
		{
			super(reservedSize, maxSize);
			_exclusionManager = new ExclusionManager();
			_dispatcher = new EventDispatcher(this);
		}
		/* INTERFACE blog.zihua2007.interfaces.IDispose */
		public function dispose():void
		{
			this.clear();
		}
		/* public function */
		public function selectedItem(data:CpVo):void
		{
			_exclusionManager.selectedItem(data);
		}
		/* override function */
		override public function append(x:Object):DLLNode
		{
			if (x is CpVo)
			{
				_exclusionManager.registerItem(x as CpVo);
			}
			else
			{
				throw new Error("不是CpVo");
			}
			var node:DLLNode=super.append(x);
			dispatchEvent(new ListModelEvent(ListModelEvent.LISTMODEL_CHANGE));
			return node;
		}

		override public function removeTail():Object
		{
			var x:Object;
			if (size() > 0)
			{
				x=super.removeTail();
			}
			else
			{
				return null;
			}
			dispatchEvent(new ListModelEvent(ListModelEvent.LISTMODEL_CHANGE));
			return x;
		}
		
		/* INTERFACE flash.events.IEventDispatcher */
		
		public function hasEventListener(type:String):Boolean 
		{
			return _dispatcher.hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean 
		{
			return _dispatcher.willTrigger(type);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void 
		{
			_dispatcher.removeEventListener(type, listener, useCapture);
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void 
		{
			_dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		public function dispatchEvent(e:Event):Boolean
		{
			return _dispatcher.dispatchEvent(e);
		}

	/* private function */
	}

}
